package com.test.stream.utils;

import com.alibaba.fastjson.JSON;
import com.test.stream.bean.Context;
import com.test.stream.bean2.RequestContext;
import com.test.stream.bean2.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class XmlBuilder {

    private static final String path = "xmlFile/bean.xml";
    /**
     * 将XML转为指定的POJO
     * @param clazz
     * @param xmlStr
     * @return
     * @throws Exception
     */
    public static <T> T xmlStrToOject(Class<T> clazz, String xmlStr) throws Exception {
        JAXBContext context = JAXBContext.newInstance(clazz);
        // XML 转为对象的接口
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Reader reader = new StringReader(xmlStr);
        // 以文件流的方式传入这个string
        Object unmarshal = unmarshaller.unmarshal(reader);
        if (null != reader) {
            reader.close();
        }
        return (T)unmarshal;
    }

    public static Context listCity() throws Exception {
        // 读取XML文件
        Resource resource = new ClassPathResource(path);
        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), "utf-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = br.readLine()) !=null) {
            buffer.append(line);
        }
        br.close();
        // XML转为Java对象
        Context cityList = XmlBuilder.xmlStrToOject(Context.class, buffer.toString());
        return cityList;
    }


    public static List<RequestContext> getRequestContexts(String resourcePath, Class<?> clazz) throws Exception{
        String input = Test.class.getClassLoader().getResource(resourcePath).getPath();
        File[] files = new File(input).listFiles();
        List<RequestContext> requestContexts = new ArrayList<>(16);
        for (int i = 0; i < files.length; i++) {
            String path = files[i].getPath();
            try {
                FileInputStream fileInputStream = new FileInputStream(new File(path));
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream,"UTF-8"));
                String line;
                StringBuffer contextBuffer = new StringBuffer();
                while ((line = bufferedReader.readLine())!=null){
                    contextBuffer.append(line);
                }
                JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                Reader reader = new StringReader(contextBuffer.toString());
                RequestContext requestContext =(RequestContext)unmarshaller.unmarshal(reader);
                requestContexts.add(requestContext);
            } catch (FileNotFoundException e) {
                throw new FileNotFoundException(resourcePath + "not found file path!");
            } catch (UnsupportedEncodingException e) {
                throw new UnsupportedEncodingException("EncodingException...");
            } catch (IOException e) {
                throw new IOException(" io exception...");
            } catch (JAXBException e) {
                throw new JAXBException("jaxb Exception...");
            }
        }
        return requestContexts;
    }



    public static void main(String[] args) throws Exception {
        Context cityList = listCity();
        System.out.println(JSON.toJSONString(cityList));
    }

}